#使用requests爬取10页豆瓣电影250
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(r'D:\bianchengerbianchenger\driver\chromedriver.exe')
driver.get('https://movie.douban.com/top250')
# driver.maximize_window()
time.sleep(1)
t: int = 1
for i in range(18):
    all_message = driver.find_elements(By.CSS_SELECTOR,'div.info')
    time.sleep(1)
    for i in all_message:
        video_name = i.find_element(By.CSS_SELECTOR, 'div.hd a span').text
        Director_and_Mainactor_and_so_on = i.find_element(By.CSS_SELECTOR,'div.bd p').text
        # segmentation1 = Director_and_Mainactor_and_so_on.split()
        director = Director_and_Mainactor_and_so_on.split('\n')[0].split('   ')[0][5]
        try :
            main_actor = Director_and_Mainactor_and_so_on.split('\n')[0].split('    ')[1][5:]

        except Exception:
            main_actor = None

        year = Director_and_Mainactor_and_so_on.split('\n')[1].split(' / ')[0]
        country = Director_and_Mainactor_and_so_on.split('\n')[1].split(' / ')[1]
        mtype = Director_and_Mainactor_and_so_on.split('\n')[1].split(' / ')[2]
        score = i.find_element(By.CSS_SELECTOR, 'span.rating_num').text
        remark_or_comment = i.find_element(By.CSS_SELECTOR, 'div.star span+span+span+span').text[:-3]


        print(t)
        print('\n'"电影名称：{}\n导演：{}\n主演：{}\n年份：{}\n国家：{}\n类型：{}\n评分：{}\n评论数量：{}\n".format(video_name,director
                                                                                           ,main_actor,year,country,mtype,score,remark_or_comment))
        t=t+1
    try:
        driver.find_element(By.LINK_TEXT,'后页').click()
    except Exception:
        print("最后一页，爬取结束")

driver.quit()